<template>
  <div class="search-scroll">
    <van-nav-bar
      left-arrow
      @click-left="$router.back()"
      title="搜索中心"
    >
    </van-nav-bar>
    <!-- 搜索框 -->
    <van-search
      class="search-bar"
      @input="throttleInput"
      v-model="value"
      show-action
      placeholder="请输入搜索关键词"
    >
      <template #action>
        <div @click="onSearch">搜索</div>
      </template>
    </van-search>
    <!-- 搜索联想 -->
    <van-cell-group v-if="suggestion.length">
      <van-cell
        icon="search"
        v-for="(sug, idx) in lightSuggestion"
        :key="idx"
      >
        <div
          v-html="sug"
          @click="hSugToHis(idx)"
        ></div>
      </van-cell>
    </van-cell-group>
    <!-- 搜索历史 -->
    <van-cell-group
      title="搜索历史"
      v-else
    >
      <van-cell
        v-for="(history, idx) in searchHistory"
        :key="idx"
        @click="$router.push('/search/result?keyword=' + history)"
      >
        {{history}}
        <template #right-icon>
          <van-icon
            @click.stop="hDelHistory(idx)"
            name="close"
          />
        </template>
      </van-cell>
    </van-cell-group>
  </div>
</template>
<script>
import { getSuggestion } from '../../api/search.js'
import { throttle } from '../../utils/throttle.js'
import { getHistory, setHistory } from '../../utils/storage searchHistory.js'
export default {
  data () {
    return {
      value: '',
      suggestion: [],
      searchHistory: getHistory() || []
    }
  },
  computed: {
    lightSuggestion () {
      const reg = new RegExp(this.value, 'gi')
      return this.suggestion.map(item => item.replace(reg, (val) => { return '<span style="color:#3296fa">' + val + '</span>' }))
    }
  },
  created () {
  },
  methods: {
    async hInput () {
      if (this.value === '') {
        this.suggestion = []
        return
      }
      try {
        const res = await getSuggestion(this.value)
        console.log(res)
        this.suggestion = res.data.data.options
      } catch (err) {
        console.log(err)
      }
    },
    // 节流函数操作
    throttleInput: throttle(function () {
      this.hInput()
    }, 500),
    // 点击搜索将搜索值添加到搜索历史
    onSearch () {
      // console.log(this.searchHistory)
      // this.searchHistory.unshift(this.value)
      // setHistory(this.searchHistory)
      this.hSaveHistory(this.value)
      this.$router.push('/search/result?keyword=' + this.value)
    },
    // 点击联想建议将建议值添加到搜索历史
    hSugToHis (idx) {
      // console.log('sug', this.suggestion[idx])
      // this.suggestion[idx]
      // this.searchHistory.unshift(this.suggestion[idx])
      // setHistory(this.searchHistory)
      this.hSaveHistory(this.suggestion[idx])
      this.$router.push('/search/result?keyword=' + this.suggestion[idx])
      // this.suggestion = []
    },
    // 点击删除搜索历史
    hDelHistory (idx) {
      // console.log('del', idx)
      this.searchHistory.splice(idx, 1)
      setHistory(this.searchHistory)
    },
    // 保存搜索历史到localstorage
    hSaveHistory (item) {
      const idx = this.searchHistory.indexOf(item)
      if (idx !== -1) {
        this.searchHistory.splice(idx, 1)
      }
      this.searchHistory.unshift(item)
      setHistory(this.searchHistory)
      // this.value = ''
      // this.suggestion = []
    }
  }
}
</script>
<style lang="less" scoped>
.search-scroll {
  height: 100%;
  overflow: auto;
  .search-bar {
    margin-top: 39px;
  }
}
</style>
